<?php
class Wlib_Model_DbMapper_AbstractDbMapper
{
    /**
     * @var string
     */
    protected $_dbTableName = null;
    
    /**
     * @var Zend_Db_Table
     */
    protected $_dbTable = null;

    /**
     * @return Zend_Db_Table
     */
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            if(null === $this->_dbTableName) {
                $this->_dbTableName = $this->createProperResourceName('DbTable');
            }

            // @todo throw exception if class not exists
            $this->setDbTable(new $this->_dbTableName);
        }

        return $this->_dbTable;
    }

    public function setDbTable($dbTable)
    {
        return $this->_dbTable = $dbTable;
    }

    public function createProperResourceName($resourceType)
    {
        $separator = '_';

        $className = get_class($this);        
        $classParts = explode($separator, $className);
        $objName = array_pop($classParts);
        array_pop($classParts);

        $dbTableName = implode($separator, $classParts)
                     . $separator . $resourceType . $separator . $objName;

        return $dbTableName;
    }
}
?>
